<?php
class ModelCommunityReport extends Model{
    public function alreadyReported($getData){
        $sql="SELECT report_id FROM " . DB_PREFIX . "community_reports WHERE report_closed='0' AND ";

        if(isset($getData['pid'])){
            $sql.="post_id='" . (int)$getData['pid'] . "'";
        }

        if(isset($getData['mid'])){
            $sql.="pm_id='" . (int)$getData['mid'] . "'";
        }

        $sql.="LIMIT 0,1";

        $query=$this->db->query($sql);

        if($query->row){
            return TRUE;
        }else{
            return FALSE;
        }
    }

    public function getReportReasons(){
        $query=$this->sql_cache->get('community.reportreasons');

        if(!$query){
            $query=$this->db->query("SELECT reason_id, reason_description FROM " . DB_PREFIX . "community_report_reasons ORDER BY reason_sort_order ASC");
            $this->sql_cache->set('community.reportreasons',$query);
        }

        return $query->rows;
    }

    public function addReport($getData,$postData){
        $fid=isset($getData['fid']) ? $getData['fid'] : 0;
        $tid=isset($getData['tid']) ? $getData['tid'] : 0;
        $pid=isset($getData['pid']) ? $getData['pid'] : 0;
        $mid=isset($getData['mid']) ? $getData['mid'] : 0;

        $this->db->query("INSERT INTO " . DB_PREFIX . "community_reports SET reason_id='" . (int)$postData['report-reason'] . "', forum_id='" . (int)$fid . "', topic_id='" . (int)$tid . "', post_id='" . (int)$pid . "', pm_id='" . (int)$mid . "', user_id='" . (int)$this->customer->getForumUserId() . "', user_notify='" . (int)$postData['notify-me'] . "', report_closed='0', report_time='" . (int)time() . "', report_text='" . $this->db->escape(trim($postData['further-info'])) . "'");

        if($pid){
            $this->db->query("UPDATE " . DB_PREFIX . "community_posts SET post_reported='1' WHERE post_id='" . (int)$pid . "'");
            $this->db->query("UPDATE " . DB_PREFIX . "community_topics SET topic_reported='1' WHERE topic_id='" . (int)$tid . "'");
        }else{
            $this->db->query("UPDATE " . DB_PREFIX . "community_privmsgs SET message_reported='1' WHERE msg_id='" . (int)$mid . "'");
        }

        //clear sql cache
        if($tid){
            $this->sql_cache->delete('viewtopic.posts.' . md5($tid));
            $this->sql_cache->delete('viewforum.announcementtopics.' . md5($fid));
            $this->sql_cache->delete('viewforum.topics.' . md5($fid));
        }
    }
}
?>